package com.mo.lib.view.textview

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView

/**
 * @ author：mo
 * @ data：2022/9/14:10:26
 * @ 功能：
 */


class RotateTextView @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null) : AppCompatTextView(context!!, attrs) {
    /**
     * 文本旋转的角度
     */
    private var mDegrees = 270
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(measuredHeight, measuredHeight)
    }

    override fun onDraw(canvas: Canvas) {
        canvas.save()
        canvas.translate(compoundPaddingLeft.toFloat(), extendedPaddingTop.toFloat())
        canvas.rotate(mDegrees.toFloat(), this.width / 2f, this.height / 2f)
        super.onDraw(canvas)
        canvas.restore()
    }

    /**
     * 改变文本选择的角度
     *
     * @param degrees 文本旋转的角度
     */
    fun setDegrees(degrees: Int) {
        mDegrees = degrees
        invalidate()
    }

    init {
        this.gravity = Gravity.CENTER
    }
}

